class Node {
    lchild
    rchild
    value
    constructor(v, l, r) {
        this.value = v;
        this.lchild = l || null;
        this.rchild = r || null;
    }
}

function treeGen(num, root) {
    if (num > 0) {
        let res = root || (new Node(10));
        let l = parseInt(100 * Math.random()),
            r = parseInt(100 * Math.random());

        res.lchild = new Node(l);
        res.rchild = new Node(r);
        treeGen(--num, res.lchild)
        treeGen(--num, res.rchild)
        return res;
    }
}

function cengxu(nodes, res = []) {
    let nextNodes = [];

    function pushLR(node) {
        res.push(node.value);
        node.lchild && nextNodes.push(node.lchild);
        node.rchild && nextNodes.push(node.rchild);
    }

    if (Array.isArray(nodes)) {
        nodes.forEach(node => {
            pushLR(node);
        })
    } else if (nodes) {
        pushLR(nodes);
    }

    return nextNodes.length ? cengxu(nextNodes, res) : res;
}

function foreSearch(node, res = []) {
    if (node) {
        res.push(node.value);
        foreSearch(node.lchild, res);
        foreSearch(node.rchild, res);
        return res;
    }
}

function midSearch(node, res = []) {
    if (node) {
        midSearch(node.lchild, res);
        res.push(node.value);
        midSearch(node.rchild, res);
        return res;
    }
}

function backSearch(node, res = []) {
    if (node) {
        backSearch(node.lchild, res);
        backSearch(node.rchild, res);
        res.push(node.value);
        return res;
    }
}

let testTree = {
    value: 'A',
    lchild: {
        value: "B",
        rchild: {
            value: "D"
        }
    },
    rchild: {
        value: "C",
        lchild: {
            value: "E"
        }
    }
}
let tree = treeGen(10);
let res = [];
console.log(cengxu(testTree));
console.log(foreSearch(testTree));
console.log(midSearch(testTree));
console.log(backSearch(testTree));